/**
 * 
 */
package com.nph.fireworks.client.mvp;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.TextMetrics;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author harveya
 * 
 */
public class CountdownView extends Composite implements
		CountdownPresenter.Display {
	@UiTemplate("CountdownView.ui.xml")
	interface CoountdownViewUiBinder extends UiBinder<Widget, CountdownView> {
	}

	@UiField
	Label countdown;

	@UiField
	HTMLPanel htmlPanel;

	private Canvas canvas;

	Ball ballArray[];

	private static CoountdownViewUiBinder uiBinder = GWT
			.create(CoountdownViewUiBinder.class);

	public CountdownView() {
		ballArray = new Ball[4];

		canvas = Canvas.createIfSupported();

		Context2d context = canvas.getContext2d();

		context.setFont("bold 12px sans-serif");
		context.setTextBaseline("top");
		// context.fillText("Hello, world", 0.0, 0.0);

		initWidget(uiBinder.createAndBindUi(this));

		htmlPanel.add(canvas);

		int w = canvas.getCoordinateSpaceWidth();
		int h = canvas.getCoordinateSpaceHeight();

		// our balls have different start coordinates, increment values
		// (speed, direction) and colors
		ballArray[0] = new Ball(w, h, 50.0, 20.0, 1.5, 2.0);
		ballArray[1] = new Ball(w, h, 60.0, 100.0, 2.0, -3.0);
		ballArray[2] = new Ball(w, h, 15.0, 70.0, -2.0, -2.5);
		ballArray[3] = new Ball(w, h, 150.0, 60.0, -2.7, -2.0);

	}

	public Widget asWidget() {
		return this;
	}

	public void setCountdownValue(String v) {
		Context2d context = canvas.getContext2d();

//		TextMetrics tm = context.measureText(v);
//
//		context.clearRect(0.0, 0.0, tm.getWidth(), 20);
		context.fillText(v, 0.0, 0.0);
	}

	class Ball {
		private int width;
		private int height;
		private double x;
		private double y;
		private double xinc;
		private double yinc;
		static final int diameter = 5;

		public Ball(int w, int h, double x, double y, double xinc, double yinc) {
			width = w;
			height = h;
			this.x = x;
			this.y = y;
			this.xinc = xinc;
			this.yinc = yinc;
		}

		public void move() {
			x += xinc;
			y += yinc;

			// when the ball bumps against a boundary, it bounces off
			if (x < 0 || x > width - diameter) {
				xinc = -xinc;
				x += xinc;
			}

			if (y < 0 || y > height - diameter) {
				yinc = -yinc;
				y += yinc;
			}
		}

		public void paint(Context2d context) {
//			context.setStrokeStyle("#000000");
			context.setFillStyle("#FFFF00");
			context.beginPath();
			context.arc(x, y, 5, 0, Math.PI * 2, true);
			context.closePath();
			context.stroke();
			context.fill();
		}

		public void unpaint(Context2d context) {
//			context.setStrokeStyle("#000000");
			context.setFillStyle("#000000");
			context.beginPath();
			context.arc(x, y, 5, 0, Math.PI * 2, true);
			context.closePath();
			context.stroke();
			context.fill();
		}
	}

	@Override
	public void update() {
		Context2d c = canvas.getContext2d();
		c.setFillStyle("#fff");
		c.fillRect(0, 0, canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight());
		for (Ball b : ballArray) {
			b.move();
			b.paint(canvas.getContext2d());
		}
	}

}
